/**
 * 项目编辑接口测试用例
 *
 * Created by snowingsea on 2020/02/09.
 */
const randomstring = require('randomstring');

const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const { statusKind } = require('./../../../../phoenix/models/projects');

module.exports = describe('/PUT projects/:id.json', function () {
  const administrator = new Administrator();
  const newUserIds = [];
  let tester;
  let project = {};
  function url(projectId) {
    if (!projectId) projectId = project.projectId;
    return `/projects/${projectId}.json`;
  }

  before(function (done) {
    administrator.createRandomTester()
      .then((res) => {
        tester = res;
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 正常请求编辑
  [
    {},
    { projectName: randomstring.generate(16) },
    { description: randomstring.generate(100) },
    { status: statusKind.frozen },
    { status: statusKind.normal },
  ].forEach((edit, index) => {
    it(`it should ${index === 0 ? 'PUT' : 'put'} a project with ${JSON.stringify(edit)}`, function (done) {
      tester.login()
        .then(() => {
          chai.request(uri)
            .put(url())
            .set('Authorization', `Bearer ${tester.token.accessToken}`)
            .query(edit)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.projectMessage(res.body);
              res.body.projectId.should.be.eql(project.projectId);
              Object.keys(edit).forEach((key) => {
                res.body[key].should.be.eql(edit[key]);
              });
              done();
            });
        })
        .catch(done);
    });
  });

  // 不同用户可存在相同项目名的项目
  it('it should put same project\'s projectName to different tester', function (done) {
    let newProjectName;
    administrator.createRandomTester()
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then(({ projectName }) => {
        newProjectName = projectName;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .put(url())
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ projectName: newProjectName })
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 由于项目名已存在而导致的编辑失败
  it('it should fail to put project\'s projectName when the name is exists', function (done) {
    let newProjectName;
    tester.createRandomProject()
      .then(({ projectName }) => {
        newProjectName = projectName;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .put(url())
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ projectName: newProjectName })
          .end((err, res) => {
            res.should.have.status(409);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可编辑不存在的项目
  it('it should fail to put a project when the project is not exists', function (done) {
    let projectId;
    tester.createRandomProject()
      .then((project) => {
        projectId = project.projectId;
        return tester.removeProject(projectId);
      })
      .then(() => {
        chai.request(uri)
          .put(url(projectId))
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ projectName: randomstring.generate(16) })
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可编辑他人的项目
  it('it should fail to put a project when the project is not yours', function (done) {
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        return newTester.createRandomProject();
      })
      .then(({ projectId }) => {
        chai.request(uri)
          .put(url(projectId))
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ projectName: randomstring.generate(16) })
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
